package cn.wildfire.chat.app;

import android.app.ActivityManager;
import android.content.Context;

import com.sourcemuch.im.caiba.HKpushsdkinterface;
import com.sourcemuch.im.caiba.InterfaceReceiversHub;

import cn.wildfire.chat.KLog;
import cn.wildfire.chat.app.third.location.viewholder.LocationMessageContentViewHolder;
import cn.wildfire.chat.kit.WfcUIKit;
import cn.wildfire.chat.kit.conversation.message.viewholder.MessageViewHolderManager;

/**
 * Create by HEKE, 2019/8/19 10:11
 * TODO 原生IM框架主进程对象
 */
public class MyApp extends BaseApp {

    public WfcUIKit wfcUIKit;

    @Override
    public void onCreate() {
        super.onCreate();
        // 优先绑定推送服务端接口
        InterfaceReceiversHub.bindHKpushsdkinterface(hKpushsdkinterface);
        // bugly，务必替换为你自己的!!!
        //CrashReport.initCrashReport(getApplicationContext(), BuildConfig.BuglyId, false);
        // 只在主进程初始化
//        if (getCurProcessName(this).equals(BuildConfig.APPLICATION_ID)) {
            KLog.e("chat-MyApp-wfcUIKit-init");
            wfcUIKit = new WfcUIKit();
            wfcUIKit.init(this);
            MessageViewHolderManager.getInstance().registerMessageViewHolder(LocationMessageContentViewHolder.class);
//        }
    }

    public static String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
                .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }

    public WfcUIKit getWfcUIKit() {
        return wfcUIKit;
    }

    /**
     * TODO 连接推送服务接口
     */
    private HKpushsdkinterface hKpushsdkinterface = new HKpushsdkinterface() {

        @Override
        public void xMServiceInitialized(boolean hmrun) {
            try {

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void receiveMessage(String message, int type) {
            getMainHandler().post(new Runnable() {
                @Override
                public void run() {
                    MyApp.this.receiveMessage(message, type);
                }
            });
        }

    };

    /**
     * Create by HEKE, 2019/8/24 15:31
     * TODO 提供子类重定向处理数据
     */
    public void receiveMessage(String message, int type) {
    }

}
